<template>
  <div style="width: 100%; padding: 5px">
    <el-card class="box-card" shadow="always" v-loading="loading">
      <template #header>
        <div class="card-header">
          <div class="title">
            <el-icon
              style="transform: translateY(4px); margin-right: 6px"
              size="20px"
              ><Fries
            /></el-icon>
            <span>校园活动</span>
          </div>

          <el-button class="button" text @click="refreshList">
            <template #icon>
              <el-icon><Refresh /></el-icon>
            </template>
          </el-button>
        </div>
      </template>
      <div v-if="total !== 0">
        <div>
          <div>
            <el-row>
              <el-col
                :span="4"
                v-for="activity in activityList1"
                :key="activity.activityId"
              >
                <div style="padding: 1px">
                  <ActivityCard :activity-info="activity" />
                </div>
              </el-col>
            </el-row>
          </div>
          <div v-if="activityList && activityList.length > 6">
            <el-row>
              <el-col
                :span="4"
                v-for="activity in activityList2"
                :key="activity.activityId"
              >
                <div style="padding: 1px">
                  <ActivityCard :activity-info="activity" />
                </div>
              </el-col>
            </el-row>
          </div>
        </div>
        <pagination
          v-if="total > 0"
          v-model:total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="queryActivityList"
        />
      </div>
      <el-empty v-else description="暂无活动" />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { Refresh } from "@element-plus/icons-vue";
import { ref, onMounted, reactive } from "vue";
import { IActivity, IPage } from "@/api/business/activity/types";
import { getPageActivityAPI } from "@/api/business/activity";
import ActivityCard from "@/views/dashboard/components/student/components/home-activity/activity-card.vue";
import { Fries } from "@element-plus/icons-vue";

//-----------组件变量-----------
const loading = ref(false);
const activityList = ref<IActivity[] | null>([]);
const activityList1 = ref<IActivity[] | null>([]);
const activityList2 = ref<IActivity[] | null>([]);
const total = ref<number>(0);
const queryParams = reactive<IPage>({
  pageNum: 1,
  pageSize: 12,
  student: true,
});

//-----------数据获取-----------
/**
 * 查询活动列表
 */
const queryActivityList = () => {
  loading.value = true;
  getPageActivityAPI(queryParams)
    .then(({ data }) => {
      activityList.value = data.list;
      if (activityList.value.length > 6) {
        activityList1.value = activityList.value.slice(0, 6);
        activityList2.value = activityList.value.slice(6);
      } else {
        activityList1.value = activityList.value;
      }
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
};

onMounted(() => {
  queryActivityList();
});

//-----------组件操作-----------
/**
 * 刷新列表当前页数据
 */
const refreshList = () => {
  queryActivityList();
};
</script>

<style scoped lang="scss">
.box-card {
  border: 1px solid rgb(235, 196, 196);
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .title {
    background-color: #a53233;
    padding: 6px 10px;
    font-size: 20px;
    color: #fff;
  }
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.box-card {
  width: 100%;
}
</style>
